"""
Case Type   : dolphin
Case Name   : 创建dolphin插件B库
Create At   : 2024.9.19
Owner       : @wan005
Description :
    1、创建兼容b库
    2、b库下查询dolphin插件
Expect      :
    1、成功
    2、自动加载成功,不用手动创建
History     :
    modified by @wan005，2022.08.22，om数据库目前实现自动加载dolphin插件，
    因此删除用例中拷贝插件依赖文件步骤
    modified by @wan005 2022/11/15: 修改用例日期，触发执行。
    modified by @kyeleze 2024/4/11: 修改用例日期，触发执行。
    modified by @zou_jialiang050 2024/4/30: 修改dolphin.b_compatibility_mode默认值。
    modified by @li-xin12345 2024/5/1: 修改用例日期，触发执行。
    modified by @liu-tong-8848 2024/6/29: 修改用例日期，触发执行。
    modified by @songjing20 2024/9/19: 修改用例日期，触发执行.
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class CompatibilityTest001(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.common = Common()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.user_node = Node('PrimaryDbUser')
        self.constant = Constant()

    def test_dolphin(self):
        self.log.info(f'----{os.path.basename(__file__)} start----')
        text = '----step1:创建和node.yml同名且兼容b库的数据库;expect:创建成功----'
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.user_node.db_name};" \
                  f"create database {self.user_node.db_name} " \
                  f"dbcompatibility ='B';"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd, dbname='postgres')
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, sql_res,
                      "执行失败" + text)

        text = '----step2:b库下查询dolphin插件;expect:自动加载成功,无需手动创建----'
        self.log.info(text)
        sql_cmd = "select extname from pg_extension where extname ='dolphin';"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=self.user_node.db_name)
        self.log.info(sql_res)
        self.assertEqual('dolphin', sql_res.splitlines()[-2].strip(),
                         '执行失败' + text)
        text = '----step3:修改dolphin.b_compatibility_mode默认值;expect:成功----'
        self.log.info(text)
        cmd = (f"alter database {self.user_node.db_name} set "
               f"dolphin.b_compatibility_mode=off;")
        self.log.info(cmd)
        sql_res = self.sh_primary.execut_db_sql(cmd,
                                                dbname='postgres')
        self.log.info(sql_res)
        self.assertIn('ALTER DATABASE', sql_res, '执行失败' + text)
        sql_cmd = f"show dolphin.b_compatibility_mode;"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=self.user_node.db_name)
        self.log.info(sql_res)
        self.assertIn('off', sql_res, '执行失败' + text)

    def tearDown(self):
        self.log.info('----无须清理环境，待sql用例执行完后清理即可----')
        self.log.info(f'----{os.path.basename(__file__)} end-----')


